<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/img/heima.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="quitBtn">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggleBtn" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="pathIndex"
          class="el-menu-vertical-demo"
          background-color="#313642"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in leftMenu"
            :key="item.id"
          >
            <!-- 一级菜单的文本 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 子菜单 -->
            <el-menu-item
              :index="'/' + val.path"
              v-for="val in item.children"
              :key="val.id"
              @click="saveIndex(val.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ val.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      leftMenu: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 设置菜单折叠与展开,默认不折叠
      isCollapse: false,
      // 设置导航栏选选中高亮
      pathIndex: ''
    }
  },
  methods: {
    // 退出功能
    quitBtn() {
      // 删除token字符
      sessionStorage.clear()
      // 强制跳转到login页面
      this.$router.push('/login')
    },
    // 点击按钮实现菜单栏的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击导航时保存并更改index的值
    saveIndex(path) {
      sessionStorage.setItem('path', '/' + path)
      this.pathIndex = sessionStorage.getItem('path')
    },
    // 得到菜单栏的原始数据
    async gitMenus() {
      const res = await this.$http.get('/menus')
      if (res.meta.status !== 200) {
        return this.$message.error('响应左侧导航栏数据失败')
      }
      this.leftMenu = res.data
    }
  },
  mounted() {
    this.gitMenus()
    this.pathIndex = sessionStorage.getItem('path')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  width: 100%;
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #353c3f;
  color: #fff;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  img {
    // 给图片加
    vertical-align: middle;
  }
  span {
    margin-left: 15px;
  }
}
.el-aside {
  width: auto;
  background-color: #313642;
  .toggleBtn {
    color: #fff;
    text-align: center;
    line-height: 24px;
    font-size: 10px;
    background-color: #4a5064;
    cursor: pointer;
    letter-spacing: 0.2em;
  }
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #e9edf0;
}
// 文本和icon的间距
.iconfont {
  margin-right: 10px;
}
</style>
